package com.yingxin.yxerp.module.permission.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.constant.CommonConst;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.commons.exception.BizException;
import com.yingxin.yxerp.module.oplog.aop.OplogAnnotation;
import com.yingxin.yxerp.module.permission.entity.Permission;
import com.yingxin.yxerp.module.permission.mapper.PermissionMapper;
import com.yingxin.yxerp.module.permission.service.IPermissionService;
import com.yingxin.yxerp.module.permission.vo.req.PermissionQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author fanchao
 * @since 2021-05-10
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    /**
     * 根据 permissionIds 查询
     *
     * @param permissionIds
     * @return
     */
    @Override
    public List<Permission> listByPermissionIds(Collection<String> permissionIds) {
        if(CollectionUtil.isEmpty(permissionIds)){
            return new ArrayList<>();
        }
        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.lambda().in(Permission::getId, permissionIds);
        qw.lambda().eq(Permission::getStatus, CommonConst.TRUE);
        return list(qw);
    }

    @Override
    public List<Permission> listByParentId(String parentId) {
        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.lambda().eq(Permission::getParentId, parentId);
        qw.lambda().orderByAsc(Permission::getId);
        return list(qw);
    }

    /**
     * 启用权限
     */
    @OplogAnnotation(biz = BusinessType.PERMISSION, id = "{{permission.id}}", action = "启用",
            desc = "权限[{{permission.name}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void enable(Permission permission) {
        UpdateWrapper<Permission> uw = new UpdateWrapper<>();
        uw.lambda().set(Permission::getStatus, CommonConst.TRUE);
        uw.lambda().eq(Permission::getId, permission.getId());
        boolean success = update(uw);
        if (!success) {
            throw new BizException("操作失败");
        }
    }

    /**
     * 禁用权限
     */
    @OplogAnnotation(biz = BusinessType.PERMISSION, id = "{{permission.id}}", action = "禁用",
            desc = "权限[{{permission.name}}]")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void disable(Permission permission) {
        UpdateWrapper<Permission> uw = new UpdateWrapper<>();
        uw.lambda().set(Permission::getStatus, CommonConst.FALSE);
        uw.lambda().eq(Permission::getId, permission.getId());
        boolean success = update(uw);
        if (!success) {
            throw new BizException("操作失败");
        }
    }

    /**
     * 验证 user id 是否存在指定的 permissionString 权限
     * false 不存在，true 存在
     */
    @Override
    public boolean checkPermissionByUserIdAndPermissionString( PermissionQueryVo queryVo ){
        List<String> ids = baseMapper.listPermissionByUserIdAndPermissionString( queryVo );
        if( CollectionUtil.isEmpty( ids ) ){
            return false;
        }
        return true;
    }

}
